﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="User_List.aspx.cs" Inherits="ncp.Web.cms.User_List" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <link rel="stylesheet" type="text/css" href="../../themes/default/easyui.css" />
    <link rel="stylesheet" type="text/css" href="../../themes/icon.css" />
	<script type="text/javascript" src="../../js/jquery-1.7.1.min.js"></script>
	<script type="text/javascript" src="../../js/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../../js/locale/easyui-lang-zh_CN.js"></script>
	<script type="text/javascript" src="../../../js/common.js"></script>
	<script type="text/javascript" src="../../../js/calendar.js"></script>
</head>
<body style="margin:0; padding:0px;">
    <form id="form1" runat="server">
        <div class="toolbar" style="background-color:#efefef">
	        <a href="#" class="easyui-linkbutton" plain="true" iconCls="icon-add" onclick="onAdd();">增加</a>
	        <a href="#" class="easyui-linkbutton" plain="true" iconCls="icon-remove" onclick="onDelete();">删除</a>
	        <a href="#" class="easyui-linkbutton" plain="true" iconCls="icon-edit" onclick="onEdit();">编辑</a>
	        <a href="#" class="easyui-linkbutton" plain="true" iconCls="icon-edit" onclick="onEditPassword();">修改密码</a>
        </div>
        <div id="divright">    
        </div>
	    <div id="divBox" class="easyui-window" title="用户信息" iconCls="icon-search" resizable="true" collapsible="true" minimizable="false" maximizable="true" closable="true">
	        <p></p>
	        <table width="100%" cellpadding="3" cellspacing="0" border="0">
	            <tr>
                    <td>
                        <span style="padding:10px;">用户帐号：</span><input id="txtUser_Account" type="text" maxlength="25" />
                    </td>
	                <td>
	                    <span style="padding:10px;">用户名称：</span><input id="txtUser_Name" type="text" maxlength="100" />
	                </td>
	            </tr>
	            <tr>
	                <td>
	                    <span style="padding:10px;">手机：</span><input id="txtUser_Mobile" type="text" maxlength="15" />
	                </td>
	                <td>
	                    <span style="padding:10px;">邮箱：</span><input id="txtUser_Email" type="text" maxlength="50" />
	                </td>
	            </tr>
	            <tr id="tr1">
	                <td>
	                    <span style="padding:10px;">密码：</span><input id="txtUser_Password1" type="password" maxlength="15" />
	                </td>
	                <td>
	                    <span style="padding:10px;">确认密码：</span><input id="txtUser_Password2" type="password" maxlength="15" />
	                </td>
	            </tr>
	            <tr>
	                <td>
	                    <span style="padding:10px;">注册时间：</span><input id="txtUser_RegsiterDate" type="text" readonly="readonly" onclick="setday(this);" />
	                </td>
	                <td>
	                    <span style="padding:10px;">状态：</span>
	                    <input id="radioUser_Status1" name="radioUser_Status" type="radio" value="1" checked="checked" />启用
	                    <input id="radioUser_Status2" name="radioUser_Status" type="radio" value="0" />禁用
	                </td>
	            </tr>
	            <tr>
	                <td colspan="2" align="center">
	                    <a href="#" class="easyui-linkbutton" onclick="onSave();">确定</a>&nbsp;&nbsp;
	                    <a href="#" class="easyui-linkbutton" onclick="onClose();">关闭</a>
	                </td>
	            </tr>
	        </table>
		</div>
	    <div id="divPwsBox" class="easyui-window" title="修改用户密码" iconCls="icon-search" resizable="true" collapsible="true" minimizable="false" maximizable="true" closable="true">
	        <p></p>
	        <table width="100%" cellpadding="3" cellspacing="0" border="0">
	            <tr id="tr2">
	                <td>
	                    <span style="padding:10px;">密码：</span><input id="Password1" type="password" maxlength="15" />
	                </td>
	            </tr>
	            <tr>
	                <td>
	                    <span style="padding:10px;">确认密码：</span><input id="Password2" type="password" maxlength="15" />
	                </td>
	            </tr>
	            <tr>
	                <td align="center">
	                    <a href="#" class="easyui-linkbutton" onclick="onSavePws();">确定</a>&nbsp;&nbsp;
	                    <a href="#" class="easyui-linkbutton" onclick="onClosePws();">关闭</a>
	                </td>
	            </tr>
	        </table>
		</div>		
	</form>
</body>
</html>
<script type="text/javascript">
    var pageobj = new Object();
    $(function() {
        initDialog();
        initPageObj();
        initDatagrid();
        LoadUserInfo();
    });

    //初始化对话框
    function initDialog() {
        $('#divBox').dialog({
            modal: true,
            shadow: true,
            width: 600,
            height: 250,
            padding: 10,
            closed: true
        });
        $('#divPwsBox').dialog({
            modal: true,
            shadow: true,
            width: 400,
            height: 200,
            padding: 10,
            closed: true
        });
    }
    
    //初始化页码对象
    function initPageObj() {
        pageobj.currentpageindex = 1;
        pageobj.pagesize = 10;
        pageobj.sorting = "User_RegsiterDate";
        pageobj.sortdir = "ASC";
        pageobj.currentwhere = "";
        pageobj.recordcount = 0;
    }

    //初始化表格
    function initDatagrid() {
        $('#divright').datagrid({
            width: 'auto',
            height: 350,
            nowrap: true,
            striped: true,
            singleSelect: true,
            sortName: 'User_RegsiterDate',
            sortOrder: 'asc',
            remoteSort: false,
            idField: 'User_Id',
            pagination: false,
            rownumbers: true,
            columns: [[
					{ field: 'User_Account', title: '用户帐号', sortable: 'true', width: 80 },
					{ field: 'User_Name', title: '用户名称', sortable: 'true', width: 120 },
					{ field: 'User_Email', title: '邮箱', sortable: 'true', width: 120, align: 'center' },
					{ field: 'User_Mobile', title: '手机', sortable: 'true', width: 120, align: 'center' },
					{ field: 'User_RegsiterDate', title: '注册时间', sortable: 'true', width: 120, align: 'center',
					    formatter: function(value, rec) {
					        return value.replace(" 00:00:00", "");
					    }
					},
					{ field: 'User_Status', title: '状态', sortable: 'true', width: 60, align: 'center',
					    styler: function(value, row, index) {
					        if (value == '0') {
					            return 'background-color:#6293BB;color:#fff;font-weight:bold;';
					        }
					    },
					    formatter: function(value, rec) {
					        if (value == '0') {
					            return '禁用';
					        }
					        else {
					            return '启用';
					        }
					    }
					},
					{ field: 'User_Id', title: '操作', sortable: 'true', width: 60, align: 'center',
					    formatter: function(value, rec, rowIndex) {
					        if (rec.User_Status == '0')
					            return "<a href=\"#\" class=\"easyui-linkbutton\" onclick=\"onSeting(" + rowIndex + "," + value + ",1);\">启用</a>";
					        else
					            return "<a href=\"#\" class=\"easyui-linkbutton\" onclick=\"onSeting(" + rowIndex + "," + value + ",0);\">禁用</a>";
					    }
					}
				]]
        });
    }


    //装载用户信息视图
    function LoadUserInfo() {
        var jsonStr = $.objectToJson(pageobj);
        var data = eval(ncp.Web.cms.User_List.BindTable(jsonStr).value);
        $('#divright').datagrid('loadData', data);
    }

    //增加用户
    function onAdd() {
        $('#divBox').window('setTitle', '添加用户');
        $('#divBox').window("open");

        $("#divBox input").val("");
        //$('#divBox').form('clear');//清空
        $("#divBox").attr("cmd", "Add");
        $("#tr1").css("display","");
        return false;
    }

    //编辑用户
    function onEdit() {
        var selected = $('#divright').datagrid('getSelected');
        if (selected) {
            $('#divBox').window('setTitle', '编辑用户');
            $('#divBox').window("open");
            $("#divBox").attr("cmd", "Edit");
            $("#divBox").attr("user_id", selected.User_Id);
            $("#txtUser_Account").val(selected.User_Account);
            $("#txtUser_Name").val(selected.User_Name);
            $("#txtUser_Mobile").val(selected.User_Mobile);
            $("#txtUser_Email").val(selected.User_Email);
            $("#txtUser_Password1").val(selected.User_Password);
            $("#txtUser_Password2").val(selected.User_Password);
            $("#txtUser_RegsiterDate").val(selected.User_RegsiterDate);
            $("input[name='radioUser_Status'][value='" + selected.User_Status + "']").attr("checked", "true");
            $("#tr1").css("display","none");
        }
        else
            alert("请先选择一条记录！");
        return false;
    }

    //编辑用户密码
    function onEditPassword() {
        var selected = $('#divright').datagrid('getSelected');
        if (selected) {
            $('#divPwsBox').window('setTitle', '修改用户密码');
            $('#divPwsBox').window("open");
            $("#divPwsBox").attr("user_id", selected.User_Id);
            $("#Password1").val();
            $("#Password2").val();
        }
        else
            alert("请先选择一条记录！");
        return false;
    }
    
    //删除用户
    function onDelete() {
        var selected = $('#divright').datagrid('getSelected');
        if (selected) {
            if (confirm("您确定要删除吗？")) {
                var response = ncp.Web.cms.User_List.DeleteUserInfo(selected.User_Id);
                if (response.value != null) {
                    var jsonOBJ = eval(response.value)[0];
                    if (jsonOBJ.isreload) {
                        var index = $('#divright').datagrid('getRowIndex', selected);
                        $('#divright').datagrid('deleteRow', index);
                    }
                    alert(jsonOBJ.msg);
                }
            }
        }
        else
            alert("请先选择一条记录！");
        return false;
    }
    
    //设置是否启用
    function onSeting(rowIndex,User_Id, User_Status) {
        $('#divright').datagrid('selectRow', rowIndex);
        var selected = $('#divright').datagrid('getSelected');
        if (User_Status == 1) var str = "启用";
        else var str = "禁用";
        if (confirm("您确定要" + str + "吗？")) {
            var response = ncp.Web.cms.User_List.UpdateUserStatus(User_Id, User_Status);
            if (response.value != null) {
                var jsonOBJ = eval(response.value)[0];
                selected.User_Status = User_Status;
                $('#divright').datagrid('refreshRow', rowIndex);
                alert(jsonOBJ.msg);
            }
        }
        return false;
    }

    //保存用户信息
    function onSave() {
        if ($("#txtUser_Account").val() == "") {
            alert("用户帐号不能为空！");
            return false;
        }
        if ($("#txtUser_Name").val() == "") {
            alert("用户名称不能为空！");
            return false;
        }
        if ($("#txtUser_Account").val() == "") {
            alert("用户帐号不能为空！");
            return false;
        }
        if ($("#txtUser_Password1").val() == "") {
            alert("密码不能为空！");
            return false;
        }
        if ($("#txtUser_Password1").val() != $("#txtUser_Password2").val()) {
            alert("二次密码不一样！");
            return false;
        }
        var obj = new Object();
        obj.cmd = $("#divBox").attr("cmd");
        obj.user_id = $("#divBox").attr("user_id");
        obj.user_account = $("#txtUser_Account").val();
        obj.user_name = $("#txtUser_Name").val();
        obj.user_mobile = $("#txtUser_Mobile").val();
        obj.user_email = $("#txtUser_Email").val();
        obj.user_password = $("#txtUser_Password1").val();
        obj.user_regsiterdate = $("#txtUser_RegsiterDate").val();
        obj.user_role = "system";
        obj.user_status = $("input[name='radioUser_Status']:checked").attr("value");

        var jsonStr = $.objectToJson(obj);
        var response = ncp.Web.cms.User_List.SaveUserInfo(jsonStr);
        if (response.value != null) {
            var jsonOBJ = eval(response.value)[0];
            if (jsonOBJ.isreload) {
                $('#divBox').dialog("close");
                if (obj.cmd == "Add")
                    appendRow(jsonOBJ.user_id, obj);
                else
                    updateRow(obj);
            }
            else
                alert(jsonOBJ.msg);
        }
        return false;
    }

    //保存用户信息
    function onSavePws() {
        if ($("#Password1").val() == "") {
            alert("密码不能为空！");
            return false;
        }
        if ($("#Password1").val() != $("#Password2").val()) {
            alert("二次密码不一样！");
            return false;
        }
        var response = ncp.Web.cms.User_List.UpdateUserPassword($("#divPwsBox").attr("user_id"), $("#Password1").val());
        if (response.value != null) {
            var jsonOBJ = eval(response.value)[0];
            alert(jsonOBJ.msg);
            $('#divPwsBox').dialog("close");
        }
        return false;
    }
    
    //增加一条商品记录
    function appendRow(myid, obj) {
        $('#divright').datagrid('appendRow', {
            User_Id: myid,
            User_Account: obj.user_account,
            User_Name: obj.user_name,
            User_Email: obj.user_email,
            User_Mobile: obj.user_mobile,
            User_RegsiterDate: obj.user_regsiterdate,
            User_State: obj.user_state
        });
    }

    //修改一条商品记录
    function updateRow(obj) {
        var selected = $('#divright').datagrid('getSelected');
        if (selected) {
            var index = $('#divright').datagrid('getRowIndex', selected);
            selected.User_Id = obj.user_id;
            selected.User_Account = obj.user_account;
            selected.user_name = obj.user_name;
            selected.User_Email = obj.user_email;
            selected.User_Mobile = obj.user_mobile;
            selected.User_RegsiterDate = obj.user_regsiterdate;
            selected.User_Status = obj.user_status;
            $('#divright').datagrid('refreshRow', index);
        }
    }
    
    //关闭用户信息对话框
    function onClose() {
        $('#divBox').window("close");
    }

    function onClosePws() {
        $('#divPwsBox').window("close");
    }
</script>
